package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 保存菜品信息和口味信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveDishWithFlavor(DishDto dishDto) {
        //1.储存菜品信息
        this.save(dishDto);
        Long dishId = dishDto.getId();
        //2.给口味信息赋菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().forEach((flavor) ->{
            flavor.setDishId(dishId);
        });
        //3.储存口味信息
        dishFlavorService.saveBatch(flavors);


        return;


    }

    @Override
    public DishDto getDishByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavors = dishFlavorService.list(lqw);
        DishDto dishDto = new DishDto();

        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    @Override
    @Transactional
    public void updateDishWithFlavor(DishDto dishDto) {
        //1.修改dish表
        this.updateById(dishDto);
        //2.删除dish_flavor里的口味信息
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lqw);
        //3.新增
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().forEach((flavor) ->{
            flavor.setDishId(dishDto.getId());
        });

        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 删除菜品和口味信息
     * @param ids
     */
    @Transactional
    @Override
    public void deleteDishWithFlavor(List<Long> ids) {
        //查询是否停售
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ids != null,Dish::getId,ids);
        lambdaQueryWrapper.eq(Dish::getStatus,1);

        int count = this.count(lambdaQueryWrapper);

        if (count > 0){
            throw new CustomException("有菜品未停售，无法删除");
        }


        //1、删除dish表
        this.removeByIds(ids);

        //2、删除dish_flavor
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids != null,DishFlavor::getDishId,ids);

        dishFlavorService.remove(lqw);

    }
}
